home *** CD-ROM | disk | FTP | other *** search
/ Champak 66 / Vol 66.iso / games / abstract.swf / scripts / __Packages / mochi / MochiServices.as < prev   
Text File  |  2013-04-24  |  11KB  |  314 lines

  1. class mochi.MochiServices
  2. {
  3.    static var _gatewayURL = "http://www.mochiads.com/static/lib/services/services.swf";
  4.    static var _listenChannelName = "__mochiservices";
  5.    static var _connecting = false;
  6.    static var _connected = false;
  7.    function MochiServices()
  8.    {
  9.    }
  10.    static function get id()
  11.    {
  12.       return mochi.MochiServices._id;
  13.    }
  14.    static function get clip()
  15.    {
  16.       return mochi.MochiServices._container;
  17.    }
  18.    static function get childClip()
  19.    {
  20.       return mochi.MochiServices._clip;
  21.    }
  22.    static function getVersion()
  23.    {
  24.       return "1.31";
  25.    }
  26.    static function allowDomains(server)
  27.    {
  28.       var _loc1_ = server.split("/")[2].split(":")[0];
  29.       if(System.security)
  30.       {
  31.          if(System.security.allowDomain)
  32.          {
  33.             System.security.allowDomain("*");
  34.             System.security.allowDomain(_loc1_);
  35.          }
  36.          if(System.security.allowInsecureDomain)
  37.          {
  38.             System.security.allowInsecureDomain("*");
  39.             System.security.allowInsecureDomain(_loc1_);
  40.          }
  41.       }
  42.       return _loc1_;
  43.    }
  44.    static function get isNetworkAvailable()
  45.    {
  46.       if(System.security)
  47.       {
  48.          var _loc1_ = System.security;
  49.          if(_loc1_.sandboxType == "localWithFile")
  50.          {
  51.             return false;
  52.          }
  53.       }
  54.       return true;
  55.    }
  56.    static function set comChannelName(val)
  57.    {
  58.       if(val != undefined)
  59.       {
  60.          if(val.length > 3)
  61.          {
  62.             mochi.MochiServices._sendChannelName = val + "_fromgame";
  63.             mochi.MochiServices._rcvChannelName = val;
  64.             mochi.MochiServices.initComChannels();
  65.          }
  66.       }
  67.    }
  68.    static function get connected()
  69.    {
  70.       return mochi.MochiServices._connected;
  71.    }
  72.    static function connect(id, clip, onError)
  73.    {
  74.       if(!mochi.MochiServices._connected && mochi.MochiServices._clip == undefined)
  75.       {
  76.          trace("MochiServices Connecting...");
  77.          mochi.MochiServices._connecting = true;
  78.          mochi.MochiServices.init(id,clip);
  79.       }
  80.       if(onError != undefined)
  81.       {
  82.          mochi.MochiServices.onError = onError;
  83.       }
  84.       else if(mochi.MochiServices.onError == undefined)
  85.       {
  86.          mochi.MochiServices.onError = function(errorCode)
  87.          {
  88.             trace(errorCode);
  89.          };
  90.       }
  91.    }
  92.    static function disconnect()
  93.    {
  94.       if(mochi.MochiServices._connected || mochi.MochiServices._connecting)
  95.       {
  96.          mochi.MochiServices._connecting = mochi.MochiServices._connected = false;
  97.          mochi.MochiServices.flush(true);
  98.          if(mochi.MochiServices._clip != undefined)
  99.          {
  100.             mochi.MochiServices._clip.removeMovieClip();
  101.             delete mochi.MochiServices._clip;
  102.          }
  103.          mochi.MochiServices._listenChannel.close();
  104.          mochi.MochiServices._rcvChannel.close();
  105.       }
  106.    }
  107.    static function init(id, clip)
  108.    {
  109.       mochi.MochiServices._id = id;
  110.       if(clip != undefined)
  111.       {
  112.          mochi.MochiServices._container = clip;
  113.       }
  114.       else
  115.       {
  116.          mochi.MochiServices._container = _root;
  117.       }
  118.       mochi.MochiServices.loadCommunicator(id,mochi.MochiServices._container);
  119.    }
  120.    static function loadCommunicator(id, clip)
  121.    {
  122.       var _loc2_ = "_mochiservices_com_" + id;
  123.       if(mochi.MochiServices._clip != null)
  124.       {
  125.          return mochi.MochiServices._clip;
  126.       }
  127.       if(!mochi.MochiServices.__get__isNetworkAvailable())
  128.       {
  129.          return null;
  130.       }
  131.       mochi.MochiServices.allowDomains(mochi.MochiServices._gatewayURL);
  132.       mochi.MochiServices._clip = clip.createEmptyMovieClip(_loc2_,10336,false);
  133.       mochi.MochiServices._loader = new MovieClipLoader();
  134.       if(mochi.MochiServices._loaderListener.waitInterval != null)
  135.       {
  136.          clearInterval(mochi.MochiServices._loaderListener.waitInterval);
  137.       }
  138.       mochi.MochiServices._loaderListener = {};
  139.       mochi.MochiServices._loaderListener.onLoadError = function(target_mc, errorCode, httpStatus)
  140.       {
  141.          trace("MochiServices could not load.");
  142.          mochi.MochiServices.disconnect();
  143.          mochi.MochiServices.onError.apply(null,[errorCode]);
  144.       };
  145.       mochi.MochiServices._loaderListener.onLoadStart = function(target_mc)
  146.       {
  147.          this.isLoading = true;
  148.       };
  149.       mochi.MochiServices._loaderListener.startTime = getTimer();
  150.       mochi.MochiServices._loaderListener.wait = function()
  151.       {
  152.          if(getTimer() - this.startTime > 10000)
  153.          {
  154.             if(!this.isLoading)
  155.             {
  156.                mochi.MochiServices.disconnect();
  157.                mochi.MochiServices.onError.apply(null,["IOError"]);
  158.             }
  159.             clearInterval(this.waitInterval);
  160.          }
  161.       };
  162.       mochi.MochiServices._loaderListener.waitInterval = setInterval(mochi.MochiServices._loaderListener,"wait",1000);
  163.       mochi.MochiServices._loader.addListener(mochi.MochiServices._loaderListener);
  164.       mochi.MochiServices._loader.loadClip(mochi.MochiServices._gatewayURL,mochi.MochiServices._clip);
  165.       mochi.MochiServices._sendChannel = new LocalConnection();
  166.       mochi.MochiServices._sendChannel._queue = [];
  167.       mochi.MochiServices._rcvChannel = new LocalConnection();
  168.       mochi.MochiServices._rcvChannel.allowDomain = function(d)
  169.       {
  170.          return true;
  171.       };
  172.       mochi.MochiServices._rcvChannel.allowInsecureDomain = mochi.MochiServices._rcvChannel.allowDomain;
  173.       mochi.MochiServices._rcvChannel._nextcallbackID = 0;
  174.       mochi.MochiServices._rcvChannel._callbacks = {};
  175.       mochi.MochiServices.listen();
  176.       return mochi.MochiServices._clip;
  177.    }
  178.    static function onStatus(infoObject)
  179.    {
  180.       if(infoObject.level === "error")
  181.       {
  182.          mochi.MochiServices._connected = false;
  183.          mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  184.       }
  185.    }
  186.    static function listen()
  187.    {
  188.       mochi.MochiServices._listenChannel = new LocalConnection();
  189.       mochi.MochiServices._listenChannel.handshake = function(args)
  190.       {
  191.          mochi.MochiServices.__set__comChannelName(args.newChannel);
  192.       };
  193.       mochi.MochiServices._listenChannel.allowDomain = function(d)
  194.       {
  195.          return true;
  196.       };
  197.       mochi.MochiServices._listenChannel.allowInsecureDomain = mochi.MochiServices._listenChannel.allowDomain;
  198.       mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  199.       trace("Waiting for MochiAds services to connect...");
  200.    }
  201.    static function initComChannels()
  202.    {
  203.       if(!mochi.MochiServices._connected)
  204.       {
  205.          mochi.MochiServices._sendChannel.onStatus = function(infoObject)
  206.          {
  207.             mochi.MochiServices.onStatus(infoObject);
  208.          };
  209.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"handshakeDone"});
  210.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"registerGame",id:mochi.MochiServices._id,clip:mochi.MochiServices._clip,version:getVersion()});
  211.          mochi.MochiServices._rcvChannel.onStatus = function(infoObject)
  212.          {
  213.             mochi.MochiServices.onStatus(infoObject);
  214.          };
  215.          mochi.MochiServices._rcvChannel.onReceive = function(pkg)
  216.          {
  217.             var _loc5_ = pkg.callbackID;
  218.             var _loc4_ = this._callbacks[_loc5_];
  219.             if(!_loc4_)
  220.             {
  221.                return undefined;
  222.             }
  223.             var _loc2_ = _loc4_.callbackMethod;
  224.             var _loc3_ = _loc4_.callbackObject;
  225.             if(_loc3_ && typeof _loc2_ == "string")
  226.             {
  227.                _loc2_ = _loc3_[_loc2_];
  228.             }
  229.             if(_loc2_ != undefined)
  230.             {
  231.                _loc2_.apply(_loc3_,pkg.args);
  232.             }
  233.             delete this._callbacks[_loc5_];
  234.          };
  235.          mochi.MochiServices._rcvChannel.onError = function()
  236.          {
  237.             mochi.MochiServices.onError.apply(null,["IOError"]);
  238.          };
  239.          mochi.MochiServices._rcvChannel.connect(mochi.MochiServices._rcvChannelName);
  240.          trace("connected!");
  241.          mochi.MochiServices._connecting = false;
  242.          mochi.MochiServices._connected = true;
  243.          mochi.MochiServices._listenChannel.close();
  244.          while(mochi.MochiServices._sendChannel._queue.length > 0)
  245.          {
  246.             mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",mochi.MochiServices._sendChannel._queue.shift());
  247.          }
  248.       }
  249.    }
  250.    static function flush(error)
  251.    {
  252.       var _loc1_ = undefined;
  253.       var _loc2_ = undefined;
  254.       while(mochi.MochiServices._sendChannel._queue.length > 0)
  255.       {
  256.          _loc1_ = mochi.MochiServices._sendChannel._queue.shift();
  257.          false;
  258.          if(_loc1_.callbackID != null)
  259.          {
  260.             _loc2_ = mochi.MochiServices._rcvChannel._callbacks[_loc1_.callbackID];
  261.          }
  262.          delete mochi.MochiServices._rcvChannel._callbacks[_loc1_.callbackID];
  263.          if(error)
  264.          {
  265.             mochi.MochiServices.handleError(_loc1_.args,_loc2_.callbackObject,_loc2_.callbackMethod);
  266.          }
  267.       }
  268.    }
  269.    static function handleError(args, callbackObject, callbackMethod)
  270.    {
  271.       if(args != null)
  272.       {
  273.          if(args.onError != null)
  274.          {
  275.             args.onError.apply(null,["NotConnected"]);
  276.          }
  277.       }
  278.       if(callbackMethod != null)
  279.       {
  280.          args = {};
  281.          args.error = true;
  282.          args.errorCode = "NotConnected";
  283.          if(callbackObject != null && typeof callbackMethod == "string")
  284.          {
  285.             callbackObject.callbackMethod(args);
  286.          }
  287.          else if(callbackMethod != null)
  288.          {
  289.             callbackMethod.apply(args);
  290.          }
  291.       }
  292.    }
  293.    static function send(methodName, args, callbackObject, callbackMethod)
  294.    {
  295.       if(mochi.MochiServices._connected)
  296.       {
  297.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:methodName,args:args,callbackID:mochi.MochiServices._rcvChannel._nextcallbackID});
  298.       }
  299.       else
  300.       {
  301.          if(mochi.MochiServices._clip == undefined || !mochi.MochiServices._connecting)
  302.          {
  303.             mochi.MochiServices.onError.apply(null,["NotConnected"]);
  304.             mochi.MochiServices.handleError(args,callbackObject,callbackMethod);
  305.             mochi.MochiServices.flush(true);
  306.             return undefined;
  307.          }
  308.          mochi.MochiServices._sendChannel._queue.push({methodName:methodName,args:args,callbackID:mochi.MochiServices._rcvChannel._nextcallbackID});
  309.       }
  310.       mochi.MochiServices._rcvChannel._callbacks[mochi.MochiServices._rcvChannel._nextcallbackID] = {callbackObject:callbackObject,callbackMethod:callbackMethod};
  311.       mochi.MochiServices._rcvChannel._nextcallbackID = mochi.MochiServices._rcvChannel._nextcallbackID + 1;
  312.    }
  313. }
  314.